﻿

(function ($) {

    $.extend({

            Cookie: {
                defaults: {
                    raw: true,
                    path: '/',
                    expires: 1, // 1 day
                    domain: document.domain,
                    secure: false
                },
                cookie: function(key, value, options) {

                    // key and at least value given, set cookie...
                    //https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js
                    /** @example $.cookie('the_cookie', 'the_value');
                * @desc Set the value of a cookie.
                * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
                * @desc Create a cookie with all available options.
                * @example $.cookie('the_cookie', 'the_value');
                * @desc Create a session cookie.
                * @example $.cookie('the_cookie', null);
                * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
                * used when the cookie was set.
                */
                    // key and at least value given, set cookie...
                    if (arguments.length > 1 && String(value) !== "[object Object]") {
                        //  options = jQuery.extend({}, options);
                        options = $.extend({ }, this.defaults, options || { });

                        if (value === null || value === undefined) {
                            options.expires = -1;
                        }

                        if (typeof options.expires === 'number') {
                            var days = options.expires, t = options.expires = new Date();
                            t.setDate(t.getDate() + days);
                        }

                        value = String(value);

                        return (document.cookie = [
                            encodeURIComponent(key), '=',
                            options.raw ? value : encodeURIComponent(value),
                            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                            options.path ? '; path=' + options.path : '; path=/',
                            options.domain ? '; domain=' + options.domain : '; domain=' + document.domain,
                            options.secure ? '; secure' : ''
                        ].join(''));
                    }

                    // key and possibly options given, get cookie...
                    options = value || { };
                    var result, decode = options.raw ? function(s) { return s; } : decodeURIComponent;
                    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : '';
                },
                add: function(key, value, options) {

                    if (options && typeof options.days === 'number') {
                        var date = new Date();
                        date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
                        options.expires = date;
                    }

                    this.cookie(key, value, options);

                },
                get: function(key) {
                    return this.cookie(key);
                },
                del: function(key) {
                    this.cookie(key, null);
                },
                remove: function(key) {
                    this.del(key);
                }

            }
        });

})(jQuery);